package BackUpAction;

import java.io.File;

import java.util.ArrayList;

import Utils.Constant;
import Utils.DbAccess;
import Utils.GenUtils;




public class BackUp{
	public static String USERNAME_REMOTE="vinh";
	public static String PASSWORD_REMOTE="vinh0123";
	public static String HOST="gamma.rolo.vn";
	public static String DESC_ROOT_FOLDER = "/bk_data/data/topnews/";
	
	public static void main(String[] args) {
		BackUp bk = new BackUp();
		ArrayList<NewsBeanFull> arr = bk.getNews(args[0]);
		for(int i=0; i < arr.size(); i++){
			NewsBeanFull bean = arr.get(i);
			System.out.println(bean.getNewsId());
			String thumbnail = bean.getThumbnail();
			String homepic = bean.getHomePic();
			String picture = bean.getPicture();
			String newsId = bean.getNewsId();
			if(bk.addToBackup(bean)){
				System.out.println("Insert news thanh cong");
				String cat = bk.getCat(newsId);
				bk.insertCat(newsId, cat);
				copyFilePicture(newsId, picture);
				copyFilePicture(newsId, homepic);
				copyFilePicture(newsId, thumbnail);
			}
		}
	
		ArrayList<ClipBeanFull> arr2 = bk.getClip(args[0]);
		for(int i=0; i < arr2.size(); i++){
			ClipBeanFull bean = arr2.get(i);
			if(bk.insertClip(bean)){
				copyFileFLV(bean.getClipId(), bean.getClipPath(), bean.getClipThumbnail());
			}
		}
		
		String []table={"home_0","home_1","home_2","home_3",
						"home_4","home_5","home_6","home_7",
						"home_8","home_9","home"};
		
		for(int i=0; i < table.length; i++){
			bk.truncateTable(table[i]);
			ArrayList<HomeBeanFull> hbf = bk.getHome(table[i]);
			for(int j=0; j < hbf.size(); j++){
				bk.insertHome(hbf.get(j), table[i]);
			}
		}
	}
	public ArrayList<NewsBeanFull> getNews(String date){
		DbAccess db = new DbAccess();
		String sql = "SELECT * FROM news " +
			"WHERE DatePublished >= ? limit 400";			
		ArrayList<NewsBeanFull> re = new ArrayList<NewsBeanFull>();
		
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setString(1, date);
			db.rs = db.stmt.executeQuery();
			while(db.rs.next()){
				NewsBeanFull bean = new NewsBeanFull();
				bean.setAutoId(db.rs.getInt("AutoId"));
				bean.setNewsId(db.rs.getString("NewsId"));				
				bean.setTitle(db.rs.getString("Title"));
				bean.setSummary(db.rs.getString("Summary"));
				bean.setContent(db.rs.getString("Content"));
				bean.setThumbnail(db.rs.getString("Thumbnail"));
				bean.setHomePic(db.rs.getString("HomePic"));
				bean.setPicture(db.rs.getString("Picture"));
				bean.setLink(db.rs.getString("Link"));
				bean.setHadImg(db.rs.getInt("HadImg"));
				bean.setDatePublished(db.rs.getString("DatePublished"));
				bean.setDateViewing(db.rs.getString("DateViewing"));
				bean.setSource(db.rs.getString("Source"));
				bean.setTempCat(db.rs.getString("TempCat"));
				bean.setStatus(db.rs.getInt("Status"));
				bean.setType(db.rs.getInt("Type"));
				bean.setKeyword(db.rs.getString("Keyword"));							
				bean.setTotalNews(db.rs.getInt("TotalNews"));
				bean.setAccessCount(db.rs.getInt("AccessCount"));
				bean.setRateLike(db.rs.getInt("RateLike"));
				bean.setPoster(db.rs.getString("Poster"));
				bean.setIsnew(db.rs.getInt("isnew"));
				re.add(bean);
			}
			return re;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			db.DBClose();
		}
	}
	public static boolean copyFilePicture(String newsId, String listFile){
		try{
			String []arr = listFile.split(",");
			for(int i=0; i < arr.length; i++){
				String source = GenUtils.genPathByAccountId(newsId)+"/"+arr[i];
				String descFolder = DESC_ROOT_FOLDER+GenUtils.genPathByAccountId2(newsId);
				if(new File(source).exists()){
					try{
						SCP.sftpFile(new File(source),
							USERNAME_REMOTE,
							PASSWORD_REMOTE, HOST,descFolder);
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return true;
	}
	public static boolean copyFileFLV(String clipId, String fileflv, String thumb){
		String source = GenUtils.genClipPath(clipId,fileflv);
		String descFolder = DESC_ROOT_FOLDER+"clip/"+GenUtils.genPathByAccountId2(clipId);
		if(new File(source).exists()){
			try{
				SCP.sftpFile(new File(source),
					USERNAME_REMOTE,
					PASSWORD_REMOTE, HOST,descFolder);
				source = Constant.FOLDER_DATA+"/"+GenUtils.genPathByAccountId2(clipId)+"/"+thumb;
				if(new File(source).exists()){
					descFolder = DESC_ROOT_FOLDER+GenUtils.genPathByAccountId2(clipId);
					SCP.sftpFile(new File(source),
							USERNAME_REMOTE,
							PASSWORD_REMOTE, HOST,descFolder);
					
					source = Constant.FOLDER_DATA+"/"+GenUtils.genPathByAccountId2(clipId)+"/"+thumb.replace(".", "thumb.");
					
					SCP.sftpFile(new File(source),
							USERNAME_REMOTE,
							PASSWORD_REMOTE, HOST,descFolder);
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}
	public String getCat(String newsId){
		DbAccess db = new DbAccess();
		String sql = "SELECT * FROM news_cat WHERE NewsId = ?";			
		String arrCat = "";
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setString(1, newsId);
			db.rs = db.stmt.executeQuery();
			while(db.rs.next()){
				arrCat+=db.rs.getString("CatId")+",";
			}
			return arrCat;
		}catch (Exception e) {
			e.printStackTrace();
			return "";
		}finally{
			db.DBClose();
		}
	}
	public boolean addToBackup(NewsBeanFull bean){
		DbAccess db = new DbAccess();
		String sql = "INSERT INTO news (`AutoId`,`NewsId`,`Title`,`Summary`,`Content`," +
				"`Thumbnail`,`HomePic`,`Picture`,`Link`,`HadImg`," +
				"`DatePublished`,`DateViewing`,`Source`,`TempCat`,`Status`," +
				"`Type`,`Keyword`,`TotalNews`,`AccessCount`," +
				"`RateLike`,`Poster`,`isnew`) " +
				"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";	
		
		try{
			db.connectTemp("192.168.1.3", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setInt(1,bean.getAutoId());
			db.stmt.setString(2,bean.getNewsId());
			db.stmt.setString(3,bean.getTitle());
			db.stmt.setString(4,bean.getSummary());
			db.stmt.setString(5,bean.getContent());
			db.stmt.setString(6,bean.getThumbnail());
			db.stmt.setString(7,bean.getHomePic());
			db.stmt.setString(8,bean.getPicture());
			db.stmt.setString(9,bean.getLink());			
			db.stmt.setInt(10,bean.getHadImg());
			db.stmt.setString(11,bean.getDatePublished());
			db.stmt.setString(12,bean.getDateViewing());
			db.stmt.setString(13,bean.getSource());
			db.stmt.setString(14,bean.getTempCat());
			db.stmt.setInt(15,bean.getStatus());
			db.stmt.setInt(16,bean.getType());
			db.stmt.setString(17,bean.getKeyword());			
			db.stmt.setInt(18,bean.getTotalNews());
			db.stmt.setInt(19,bean.getAccessCount());
			db.stmt.setInt(20,bean.getRateLike());
			db.stmt.setString(21,bean.getPoster());			
			db.stmt.setInt(22,bean.getIsnew());			
			db.stmt.executeUpdate();
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			db.DBClose();
		}
	}
	public boolean insertCat(String newsId, String arrCat){
		DbAccess db = new DbAccess();
		String sql = "INSERT INTO news_cat (NewsId,CatId) VALUES (?,?)";			
		try{
			db.connectTemp("192.168.1.3", Utils.Constant.DB);
			String arr[] = arrCat.split(",");
			for(int i=0; i < arr.length; i++){
				if(!arr[i].equals("")){
					db.stmt = db.conn.prepareStatement(sql);
					db.stmt.setString(1,newsId);
					db.stmt.setString(2,arr[i]);
					db.stmt.executeUpdate();
					db.stmt.close();
				}
			}
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			db.DBClose();
		}
	}
	public ArrayList<ClipBeanFull> getClip(String date){
		DbAccess db = new DbAccess();
		String sql = "SELECT * FROM clips " +
			"WHERE ClipDate >= ?";			
		ArrayList<ClipBeanFull> re = new ArrayList<ClipBeanFull>();
		
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setString(1, date);
			db.rs = db.stmt.executeQuery();
			while(db.rs.next()){
				ClipBeanFull clipBean = new ClipBeanFull();
				clipBean.setClipId(db.rs.getString("ClipId"));
				clipBean.setClipName(db.rs.getString("ClipName"));
				clipBean.setClipDescription(db.rs.getString("ClipDescription"));
				clipBean.setClipPath(db.rs.getString("ClipPath"));
				clipBean.setClipThumbnail(db.rs.getString("ClipThumbnail"));
				clipBean.setClipDate(db.rs.getString("ClipDate"));
				clipBean.setAccessCount(db.rs.getInt("AccessCount"));
				clipBean.setStatus(db.rs.getInt("Status"));
				clipBean.setClipCat(db.rs.getInt("ClipCat"));
				clipBean.setRateLike(db.rs.getInt("RateLike"));
				re.add(clipBean);
			}
			return re;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			db.DBClose();
		}
	}
	
	public boolean insertClip(ClipBeanFull bean){
		DbAccess db = new DbAccess();
		String sql = "INSERT INTO clips (`Clipid`,`ClipName`,`ClipDescription`,`ClipPath`,`ClipThumbnail`," +
				"`ClipDate`,`AccessCount`,`Status`,`ClipCat`,`RateLike`) " +
				"VALUES (?,?,?,?,?,?,?,?,?,?)";	
		try{
			db.connectTemp("192.168.1.3", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setString(1, bean.getClipId());
			db.stmt.setString(2, bean.getClipName());
			db.stmt.setString(3, bean.getClipDescription());
			db.stmt.setString(4, bean.getClipPath());
			db.stmt.setString(5, bean.getClipThumbnail());
			db.stmt.setString(6, bean.getClipDate());
			db.stmt.setInt(7, bean.getAccessCount());
			db.stmt.setInt(8, bean.getStatus());
			db.stmt.setInt(9, bean.getClipCat());
			db.stmt.setInt(10, bean.getRateLike());
			db.stmt.executeUpdate();
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			db.DBClose();
		}
	}
	public ArrayList<HomeBeanFull> getHome(String table){
		DbAccess db = new DbAccess();
		String sql = "SELECT * FROM  " + table;			
		ArrayList<HomeBeanFull> re = new ArrayList<HomeBeanFull>();
		
		try{
			db.connectTemp("localhost", Utils.Constant.DB);
			db.stmt = db.conn.prepareStatement(sql);
			db.rs = db.stmt.executeQuery();
			while(db.rs.next()){
				HomeBeanFull homeBean = new HomeBeanFull();
				homeBean.setPoster(db.rs.getString("Poster"));
				homeBean.setPosition(db.rs.getInt("Position"));
				homeBean.setNewsId(db.rs.getString("NewsId"));
				homeBean.setDatePublished(db.rs.getString("DatePublished"));				
				re.add(homeBean);
			}
			return re;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			db.DBClose();
		}
	}
	public boolean insertHome(HomeBeanFull homeBean, String table){
		DbAccess db = new DbAccess();
		String sql = "";	
		try{
			db.connectTemp("192.168.1.3", Utils.Constant.DB);
			sql = "INSERT INTO "+table+" (`NewsId`,`Position`,`DatePublished`,`Poster`) " +
				  "VALUES (?,?,?,?)";
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.setString(1, homeBean.getNewsId());
			db.stmt.setInt(2, homeBean.getPosition());
			db.stmt.setString(3, homeBean.getDatePublished());
			db.stmt.setString(4, homeBean.getPoster());
			db.stmt.executeUpdate();
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			db.DBClose();
		}
	}
	public boolean truncateTable(String table){
		DbAccess db = new DbAccess();
		String sql = "";	
		try{
			db.connectTemp("192.168.1.3", Utils.Constant.DB);
			sql = "TRUNCATE TABLE "+table;
			db.stmt = db.conn.prepareStatement(sql);
			db.stmt.executeUpdate();
			return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			db.DBClose();
		}
	}
}